Skip to content

Analytics: explain token use by part, kind of work and model; an honest tokens per merged PR - #662

Merged
nambok merged 2 commits into
mainfrom
token-use-breakdown
Nov 13, 2026
Merged

nambok merged 2 commits into
mainfrom
token-use-breakdown

Conversation

@nambok

@nambok nambok commented Nov 13, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

The Analytics tile "Tokens per merged PR" could not be explained from the page, and its change against the period before was not a like-for-like comparison. This adds a token_use section to GET /api/analytics that says what a period's tokens were (new input, output, cache read, cache write), what kind of work and which model used them, what a merged pull request took, and which runs and pull requests were heaviest. A number is compared with the period before only when both were recorded the same way.

How the tile was computed (unchanged fields, kept for older dashboards): summary() in services/gateway/src/routes/analytics.rs summed all four token parts of every run started in the period (failed runs, runs whose pull request is still open or closed, runs with no pull request) and divided by the distinct merged pull requests among those runs. Reviews, chat and log analysis were never in it (they are not runs). Fewer merges alone raised it.

Was the jump a change in counting? Partly, and the code cannot say how much:

Changes

  • common::usage: UsageSlice (four parts, calls, cost), slices() to read the per-feature and per-model counters every call already writes, total_slice(), and work_kind (each feature in exactly one kind of work).
  • gateway::routes::analytics::token_use (new module), returned as token_use:
    • total, daily, by_kind, by_model from the meter's DAY# items (every model call, on the day it was made). With one repository picked, or nothing metered, they come from the run records and basis is runs (no models).
    • Kinds: ticket runs (run, plan), security (security_fix, security), follow-ups (feedback: review comments, CI fixes and conflicts together, the meter does not tell them apart), reviews, chat (chat, brain, agent_ask), logs (log_analysis, alert), other. Each row lists the features lumped into it.
    • by_model: parts, calls and cache_hit_share (cache read out of all input).
    • per_merged_pr: runs started in the period whose pull request merged, every run of the period on that pull request, and nothing else; also by kind of run (ticket, security fix, alert).
    • runs.by_outcome: every run of the period in exactly one of merged, open, closed, failed, no pull request.
    • heaviest_runs, heaviest_prs (all runs on one pull request added up), with run ids and pull request links.
    • comparable / not_comparable / previous: the metered total is compared only when per-call counting covers the whole period before (first metered day read with one query); run-record numbers only when the period before starts on or after RUN_RECORDS_COMPLETE_FROM (2026-10-28), or after the install's own first whole metered day when that is later. Otherwise previous is null and the reason is named.
  • GET /api/usage (public): models, tokens by part and cache hit rate per model for the month. Tokens only, like the rest of that body. docs/api/openapi.json regenerated.

Storage: no new table, item or infrastructure. The per-feature and per-model counters are the durable aggregates, already updated with every recorded call; the page reads up to one DAY# item per day. The heaviest lists and per-merged-PR numbers use the run rows the route already loads for the period. No backfill: before the meter existed there is no per-call record to rebuild a split by kind or model from (run records keep no model, and pass traces expire after 30 days), and writing estimates into DAY# items would break what the daily check against the provider's report compares.

Notes for review:

Verification

  • cargo fmt --check --all, cargo clippy --workspace --all-targets -- -D warnings, cargo test --workspace: pass (1754 worker tests, the gateway and common suites, 0 failed).
  • New tests: the counters read back per feature and per model with parts, calls and cost, adding up to the item totals; every feature in one kind; per-merged-PR counts only the runs behind merged pull requests; fewer merges alone do not move it; split by kind of run; periods recorded differently are not compared (run records and the meter, including the part day counting starts); kinds, models and days with every part; one repository; the heaviest lists; nothing recorded.
  • Not verified live: no AWS calls were made, so the numbers on a real team were not read. How much of a real jump comes from the run record change cannot be told from code.

…r-merged-PR that counts only the runs behind merged PRs

The tile divided every run's tokens by the merged pull requests and compared
periods whose run records were written differently (before #346 a resume or
CI fix SET a run's tokens, dropping earlier invocations).

- common::usage: UsageSlice, slices() over the per-feature and per-model
  counters every call already writes, total_slice(), price_known(), and
  work_kind (each feature in exactly one kind of work).
- GET /api/analytics gains token_use: the period's total by part with cost,
  by day, by kind of work and by model (cache hit share, whether the price
  is known) from the meter's DAY# items; tokens per merged PR from the runs
  started in the period whose pull request merged, also by kind of run; run
  tokens by how the work ended; the heaviest runs and pull requests.
- A number is compared with the period before only when both were recorded
  the same way: the metered total when per-call counting covers the period
  before, run-record numbers when it starts on or after the day run records
  became complete. Otherwise previous is null and the reason is named.
- GET /api/usage gains models (tokens by part and cache hit rate per model);
  docs/api/openapi.json regenerated.

No new storage: the breakdown counters already exist per day, and nothing
before per-call counting can be split by kind or model.
Analytics shows tokens, not money, so token_use carries no cost: the cost
fields, the per-PR cost, the previous cost and price_known are gone, with
common::usage::price_known, which nothing else used. A test asserts the
answer names no cost or price.
@nambok
nambok merged commit 0a99a4a into main Nov 13, 2026
10 checks passed
@nambok
nambok deleted the token-use-breakdown branch November 13, 2026 23:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant